#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} RALAT ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

function installPkg {
  read -p "Teruskan dengan pemasangan semula? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    if [[ ! -d /usr/local/etc/v2ray ]]; then
      wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/v2ray.sh | bash
    fi
    apt-get -y -qq --purge remove v2ray &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -qq autoclean &>/dev/null
    clear
    sleep 3
    wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/v2ray.sh | bash
  else
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Membatalkan pemasangan semula ${RESET}"
    exit 1
  fi
}

function uninstallPkg {
  if systemctl list-unit-files | grep -qw 'v2ray'; then
    if [[ -n "$(pidof v2ray)" ]]; then
      systemctl stop v2ray@trojan*
      systemctl stop v2ray@vless*
      systemctl stop v2ray@vmess*
    fi
    if ! ("rm" -r '/usr/local/bin/v2ray' \
      '/usr/local/bin/v2ctl' \
      "$DAT_PATH" \
      '/etc/systemd/system/v2ray.service' \
      '/etc/systemd/system/v2ray@.service' \
      '/etc/systemd/system/v2ray.service.d' \
      '/etc/systemd/system/v2ray@.service.d'); then
      echo 'error: Failed to remove V2Ray.'
      exit 1
    else
      echo 'removed: /usr/local/bin/v2ray'
      echo 'removed: /usr/local/bin/v2ctl'
      echo "removed: $DAT_PATH"
      echo 'removed: /etc/systemd/system/v2ray.service'
      echo 'removed: /etc/systemd/system/v2ray@.service'
      echo 'removed: /etc/systemd/system/v2ray.service.d'
      echo 'removed: /etc/systemd/system/v2ray@.service.d'
      echo 'Please execute the command: systemctl disable v2ray'
      echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip"
      echo 'info: V2Ray has been removed.'
      echo 'info: If necessary, manually delete the configuration and log files.'
      if [[ -n "$JSONS_PATH" ]]; then
        echo "info: e.g., $JSONS_PATH and /var/log/v2ray/ ..."
      else
        echo "info: e.g., $JSON_PATH and /var/log/v2ray/ ..."
      fi
      exit 0
    fi
  else
    echo 'error: V2Ray is not installed.'
    exit 1
  fi
}

function configurePkg {

}

function detailPkg {

}

function v2rayMenu {
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} MENU V2RAY                                                 ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "[01] ${GREEN}reinstall${RESET}    - ${YELLOW}Reinstall v2ray package${RESET}"
  echo -e "[02] ${GREEN}uninstall${RESET}    - ${YELLOW}Uninstall v2ray package${RESET}"
  echo -e "[03] ${GREEN}configure${RESET}    - ${YELLOW}Configure v2ray package${RESET}"
  echo -e "[04] ${GREEN}detail   ${RESET}    - ${YELLOW}Show v2ray detail & status${RESET}"
  echo -e "[00] ${GREEN}quit     ${RESET}    - ${YELLOW}Exit from menu${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Masukkan pilihan anda: " _choice
    case "$_choice" in
    01 | reinstall)
      installPkg && break
      ;;
    02 | uninstall)
      uninstallPkg && break
      ;;
    03 | configure)
      configurePkg && break
      ;;
    04 | detail)
      detailPkg && break
      ;;
    00 | quit)
      exit 0
      ;;
    *)
      echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah ${RESET}"
      v2rayMenu
      ;;
    esac
  done
}

if [[ -f /etc/stunnel/stunnel.conf ]]; then
  v2rayMenu
elif [[ ! -f /etc/stunnel/stunnel.conf ]]; then
  installPkg
fi
